package cn.birdbaby.plugins.annotator;

import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;

/**
 * @author llorando 7/8/24 1:52 pm
 */
public class Context {
    private PsiClass psiClass;

    private PsiElement currentElement;

    private String packageName;
    private String parentPackageName;
    private PsiModifierList psiModifierList;
    private Editor editor;
    private Project project;
    private String entityName;
    private PsiJavaFile psiJavaFile;

    public PsiClass getPsiClass() {
        return psiClass;
    }

    public void setPsiClass(PsiClass psiClass) {
        this.psiClass = psiClass;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getParentPackageName() {
        return parentPackageName;
    }

    public void setParentPackageName(String parentPackageName) {
        this.parentPackageName = parentPackageName;
    }

    public PsiModifierList getPsiModifierList() {
        if(currentElement instanceof PsiClass) {
            return ((PsiClass) currentElement).getModifierList();
        } else if (currentElement instanceof PsiField) {
            return ((PsiField) currentElement).getModifierList();
        }else if (currentElement instanceof PsiMethod){
            return ((PsiMethod) currentElement).getModifierList();
        }else if(currentElement instanceof PsiJavaFile){
            return ((PsiJavaFile) currentElement).getClasses()[0].getModifierList();
        }{
            throw new RuntimeException("不支持的操作");
        }
    }

    public void setPsiModifierList(PsiModifierList psiModifierList) {
        this.psiModifierList = psiModifierList;
    }

    public Editor getEditor() {
        return editor;
    }

    public void setEditor(Editor editor) {
        this.editor = editor;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public String getEntityName() {
        return entityName;
    }

    public void setEntityName(String entityName) {
        this.entityName = entityName;
    }

    public PsiElement getCurrentElement() {
        return currentElement;
    }

    public void setCurrentElement(PsiElement currentElement) {
        this.currentElement = currentElement;
    }

    public PsiJavaFile getPsiJavaFile() {
        return psiJavaFile;
    }

    public void setPsiJavaFile(PsiJavaFile psiJavaFile) {
        this.psiJavaFile = psiJavaFile;
    }

    public Context(PsiElement psiElement) {
        this.currentElement=psiElement;
        this.psiJavaFile = (PsiJavaFile) psiElement;
        this.psiClass = psiJavaFile.getClasses()[0];
        this.packageName = ReadAction.compute(() -> {
            if (psiElement instanceof PsiJavaFile) {
                PsiPackageStatement psiPackageStatement = ((PsiJavaFile) psiElement).getPackageStatement();
                return psiPackageStatement.getPackageName();
            }
            return "";
        });
        this.parentPackageName = this.packageName.substring(0, this.packageName.lastIndexOf("."));
        this.psiModifierList = psiClass.getModifierList();
        this.project = psiClass.getProject();
        this.entityName = psiClass.getName();
    }
}
